<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Requests\Member\StoreMemberRequest;
use App\Http\Requests\Member\UpdateMemberRequest;
use App\Model\Member;
use App\Model\Room;
use Illuminate\Http\Request;
use DB;

class MemberController extends Controller
{


    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_xMem'=>'am-in',
            '_member'=>'am-active',
        ]);
    }
    
    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
      /*  $members=  DB::table('rooms')
            ->leftjoin('members','members.room_id','=','rooms.id')->get();
        return $list;*/

        $members=Member::orderBy('sort_order')->with('room')->paginate(config('xSystem.page_size'));
        //return $members;

        

        return view('Admin.xHotel.member.index')->with('members',$members);
    }

    private function attributes()
    {
        view()->share([
            'rooms' => Room::get_categories(),
        ]);
    }

    /**
     * 新增显示页
     */
    public function create(){
        Room::clear();
        $arr=array();
        $cate_id=Member::select('room_id')->get()->toArray();
        foreach ($cate_id as $v){
            $arr[]=$v['room_id'];
        }

        $this->attributes();
        return view('Admin.xHotel.member.create')->with('cate_id',$arr);;
    }

    /**
     * 新增
     * @param StoreMemberRequest $request
     */
    public function store(StoreMemberRequest $request)
    {
        //return $request->all();
        Member::create($request->all());

        return redirect('/member')->with('success', '添加成功');
    }

    /**
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $arr=array();
        $cate_id=Member::select('room_id')->get()->toArray();
        foreach ($cate_id as $v){
            $arr[]=$v['room_id'];
        }

        Room::clear();
        $this->attributes();
        $member = Member::find($id);

        return view('Admin.xHotel.member.edit')->with('member',$member)->with('cate_id',$arr);
    }

    /**
     * 修改
     * @param UpdateMemberRequest $request
     * @param $id
     */
    public function update(UpdateMemberRequest $request, $id)
    {
        $member = Member::find($id);
        $member->update($request->all());
        Room::clear();
        return redirect('/member')->with('success', '编辑成功');
    }

    /**
     * 删除
     * @param $id
     */
    public function destroy($id)
    {


        Member::destroy($id);

        return back()->with('success', '删除成功');

    }


    /**
     * 排序
     * @param Request $request
     */
    public function sort_order(Request $request){
        $sort_order=$request->sort_order;
        $category = Member::find($request->id);
        $category->sort_order=$sort_order;
        $category->save();
    }


}
